package com.qf.weekwork.controller;

import com.github.pagehelper.PageInfo;
import com.qf.weekwork.entity.AdminUser;
import com.qf.weekwork.service.UserService;
import com.qf.weekwork.vo.ManagerCode;
import com.qf.weekwork.vo.PageVo;
import com.qf.weekwork.vo.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    private ManagerCode managerCode = new ManagerCode();

    @RequestMapping("findAll")
    @ResponseBody
    @Secured({"ROLE_teacher"})
    public TableVo<AdminUser> findAll(PageVo pageVo, AdminUser adminUser){
        PageInfo<AdminUser> pageInfo = userService.selectAll(pageVo, adminUser);
        TableVo<AdminUser> tableVo = new TableVo<>();
        tableVo.setCode(0);
        tableVo.setCount(pageInfo.getTotal());
        tableVo.setData(pageInfo.getList());
        tableVo.setMsg("ok");
        return tableVo;
    }

    @RequestMapping("add")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin')")
    public ManagerCode add(AdminUser adminUser){
        Boolean add = userService.add(adminUser);
        managerCode.setStatus(add);
        managerCode.setMessage(add == true ?"ok":"fall");
        return managerCode;
    }

    @RequestMapping("del")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin')")
    public ManagerCode del(Integer[] ids){
        Boolean del = userService.del(ids);
        managerCode.setStatus(del);
        managerCode.setMessage(del == true ?"ok":"fall");
        return managerCode;
    }

    @RequestMapping("update")
    @ResponseBody
    @PreAuthorize("hasAuthority('user')")
    public ManagerCode update(AdminUser adminUser){
        Boolean update = userService.update(adminUser);
        managerCode.setStatus(update);
        managerCode.setMessage(update == true ?"ok":"fall");
        return managerCode;
    }
}
